import {ref} from 'vue'
import {defineStore} from 'pinia'
import {setUser, getUser, setToken, getToken, removeAll} from '@/utils/local'
import {USER_INFO, TOKEN} from '@/utils/constant'

const userStore = defineStore(USER_INFO, () => {
	const userToken = ref(getToken() || '')
	const userInfo = ref(getUser() || {})
	
	function setUserToken(token) {
		setToken(token)
		userToken.value = token
	}
	function updateUserInfo(user) {
		setUser(user)
		userInfo(user)
	}
	function removeUserAndToken() {
		removeAll()
	}
	
	return {
		userToken, userInfo,
		setUserToken, updateUserInfo, removeUserAndToken
	}
})